package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.CheckItem;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        Page<CheckItem> result = checkGroupDao.findpage(queryString);
        return new PageResult(result.getTotal(), result.getResult());
    }

    @Override
    public List<CheckItem> findall() {
        return checkGroupDao.findAll();
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);

        //将与checkGroup关联的checkitemIds添加到中间表中
        if (checkitemIds != null && checkitemIds.length > 0) {
            //遍历
            for (Integer checkitemId : checkitemIds) {
                //创建一个map集合用户保存中间表的数据
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroup.getId());
                map.put("checkitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        //判断当前检查组是否已有套餐引用
        Long count = checkGroupDao.countCheckGroupInCKGroupAndSetmeal(id);
        if(count>0){
            throw new RuntimeException("当前检查组被引用，不能删");
        }else{
            //先去将中间表清除该检查组的关联
            checkGroupDao.deleteCheckGroupAndCheckItem(id);
            checkGroupDao.delete(id);
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        return  checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupAndCheckItemById(Integer id) {
        return checkGroupDao.findCheckGroupAndCheckItemById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        //首先对t_checkGroup进行更改
        checkGroupDao.update(checkGroup);
        //将该检查组在中间表的关联清除
        checkGroupDao.deleteCheckGroupAndCheckItem(checkGroup.getId());
        //在对中间关联表进行更新
        if (checkItemIds != null && checkItemIds.length > 0) {
            for (Integer checkitemId : checkItemIds) {
                //创建一个map集合用户保存中间表的数据
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroup.getId());
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAllCheckGroups() {
        return checkGroupDao.findAllCheckGroups();

    }
}
